package com.bsb.biz.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bsb.base.common.config.Global;
import com.bsb.base.common.util.IdGen;
import com.bsb.base.common.util.StringUtils;
import com.bsb.base.model.BsbPolicyRelated;
import com.bsb.base.model.BsbPolicyRelatedExample;
import com.bsb.base.model.BsbPolicyRelatedExample.Criteria;
import com.bsb.biz.service.BsbPolicyRelatedService;

@Controller
@RequestMapping("/bsbPolicyRelated")
public class BsbPolicyRelatedController{
	
	private int defaultPageSize=10;
	
	private String redirect = "redirect:/bsbPolicyRelated/list.do";
	
	@Resource
	private BsbPolicyRelatedService bsbPolicyRelatedService;
	
	@RequiresPermissions("bsbPolicyRelated:view")
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String list(
			Model model,String policyId,
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize) {
		int maxPageNum = 0;
		if (pageSize == null) {
			pageSize = defaultPageSize;
		}
		if(StringUtils.isEmpty(policyId)){
			return "400";
		}
		BsbPolicyRelatedExample bsbPolicyRelatedExample = new BsbPolicyRelatedExample();
		Criteria criteria = bsbPolicyRelatedExample.createCriteria();
		criteria.andPolicyIdEqualTo(policyId);
		int totalCount = bsbPolicyRelatedService.countBsbPolicyRelated(bsbPolicyRelatedExample);
		if(totalCount%pageSize == 0){
			maxPageNum = totalCount/pageSize;
		}else{
			maxPageNum = totalCount/pageSize+1;
		}
		if (pageNum == null || pageNum < 1) {
			pageNum = 1;
		}else {
			if(maxPageNum > 0 && pageNum > maxPageNum) {
				pageNum = maxPageNum;
			}
		}
		bsbPolicyRelatedExample.setPageIndex((pageNum - 1) * pageSize);
		bsbPolicyRelatedExample.setPageSize(pageSize);
		bsbPolicyRelatedExample.setOrderByClause("createDate DESC");
		List<BsbPolicyRelated> bsbPolicyRelateds = bsbPolicyRelatedService.getPageBsbPolicyRelated(bsbPolicyRelatedExample);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("pageNum", pageNum);
		model.addAttribute("pageSize", pageSize);
		if(totalCount%pageSize == 0){
			model.addAttribute("maxPageNum", totalCount/pageSize);
		}else{
			model.addAttribute("maxPageNum", totalCount/pageSize+1);
		}
		model.addAttribute("bsbPolicyRelateds", bsbPolicyRelateds);
		model.addAttribute("policyId", policyId);
		return "/BsbPolicyRelated/list";
	}
	
	@RequiresPermissions("bsbPolicyRelated:create")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String toAdd(Model model,String policyId) {
		if(StringUtils.isEmpty(policyId)){
			return "400";
		}
		model.addAttribute("policyId", policyId);
		return "/BsbPolicyRelated/add";
	}

	@RequiresPermissions("bsbPolicyRelated:create")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String add(Model model, @ModelAttribute("bsbPolicyRelated") BsbPolicyRelated bsbPolicyRelated) {
		bsbPolicyRelated.setUuid(IdGen.uuid());
		bsbPolicyRelated.setCreateDate(new Date());
		bsbPolicyRelated.setCreateBy((String)SecurityUtils.getSubject().getPrincipal());
		bsbPolicyRelatedService.addBsbPolicyRelated(bsbPolicyRelated);
		return redirect+"?policyId="+bsbPolicyRelated.getPolicyId();
	}
	
	@RequiresPermissions("bsbPolicyRelated:delete")
	@RequestMapping(value = "/{uuid}/delete", method = RequestMethod.GET)
	public String del(Model model,
			@PathVariable("uuid") String uuid) {
		bsbPolicyRelatedService.delBsbPolicyRelatedByUuid(uuid);
		BsbPolicyRelated bsbPolicyRelated = bsbPolicyRelatedService.getBsbPolicyRelatedByUuid(uuid);
		return redirect+"?policyId="+bsbPolicyRelated.getPolicyId();
	}
	
	@RequiresPermissions("bsbPolicyRelated:delete")
	@RequestMapping(value = "/{uuid}/hide", method = RequestMethod.GET)
	public String hide(Model model,RedirectAttributes redirectAttributes,
			@PathVariable("uuid") String uuid) {
		BsbPolicyRelated bsbPolicyRelated = new BsbPolicyRelated();
		bsbPolicyRelated.setUuid(uuid);
		bsbPolicyRelated.setDelFlag(Global.HIDE);
		bsbPolicyRelatedService.updateBsbPolicyRelated(bsbPolicyRelated);
		BsbPolicyRelated bsbPolicyRelatedByUuid = bsbPolicyRelatedService.getBsbPolicyRelatedByUuid(uuid);
		redirectAttributes.addFlashAttribute("msg", "隐藏成功");
		return redirect+"?policyId="+bsbPolicyRelatedByUuid.getPolicyId();
	}
	@RequiresPermissions("bsbPolicyRelated:delete")
	@RequestMapping(value = "/{uuid}/show", method = RequestMethod.GET)
	public String show(Model model,RedirectAttributes redirectAttributes,
			@PathVariable("uuid") String uuid) {
		BsbPolicyRelated bsbPolicyRelated = new BsbPolicyRelated();
		bsbPolicyRelated.setUuid(uuid);
		bsbPolicyRelated.setDelFlag(Global.SHOW);
		bsbPolicyRelatedService.updateBsbPolicyRelated(bsbPolicyRelated);
		BsbPolicyRelated bsbPolicyRelatedByUuid = bsbPolicyRelatedService.getBsbPolicyRelatedByUuid(uuid);
		redirectAttributes.addFlashAttribute("msg", "显示成功");
		return redirect+"?policyId="+bsbPolicyRelatedByUuid.getPolicyId();
	}
	
	@RequiresPermissions("bsbPolicyRelated:update")
	@RequestMapping(value = "/{uuid}/update", method = RequestMethod.GET)
	public String toEdit(Model model,
			@PathVariable("uuid") String uuid) {
		BsbPolicyRelated bsbPolicyRelated = bsbPolicyRelatedService.getBsbPolicyRelatedByUuid(uuid);
		model.addAttribute("bsbPolicyRelated", bsbPolicyRelated);
		return "/BsbPolicyRelated/edit";
	}

	@RequiresPermissions("bsbPolicyRelated:update")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String edit(Model model, @ModelAttribute("bsbPolicyRelated") BsbPolicyRelated bsbPolicyRelated) {
		bsbPolicyRelated.setUpdateBy((String)SecurityUtils.getSubject().getPrincipal());
		bsbPolicyRelated.setUpdateDate(new Date());
		bsbPolicyRelatedService.updateBsbPolicyRelated(bsbPolicyRelated);
		String uuid = bsbPolicyRelated.getUuid();
		BsbPolicyRelated bsbPolicyRelatedByUuid = bsbPolicyRelatedService.getBsbPolicyRelatedByUuid(uuid);
		return redirect+"?policyId="+bsbPolicyRelatedByUuid.getPolicyId();
	}
	
	@RequiresPermissions("bsbPolicyRelated:view")
	@RequestMapping(value = "/{uuid}/view", method = RequestMethod.GET)
	public String view(Model model,
			@PathVariable("uuid") String uuid) {
		BsbPolicyRelated bsbPolicyRelated = bsbPolicyRelatedService.getBsbPolicyRelatedByUuid(uuid);
		model.addAttribute("bsbPolicyRelated", bsbPolicyRelated);
		return "/BsbPolicyRelated/view";
	}
}
